package com.pj.wisdomlogistics.mvp.frame;

import android.os.Bundle;

import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.ui.activity.BaseActivity;

import java.util.Random;


/**
 * Created by zhang on 2017/3/28.
 */

public abstract class MvpActivity<P extends ActivityPresenter> extends BaseActivity {
    protected P mPresenter;
    private String presenterKey;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        presenterKey =String.valueOf(System.currentTimeMillis()+new Random().nextInt());
        mPresenter = (P) Mvp.getInstance().getPresenter(presenterKey,Mvp.getGenericType(this, 0));
        mPresenter.attachView(getBaseView());
        mPresenter.onCreate();

        super.onCreate(savedInstanceState);
    }

    protected abstract BaseView getBaseView();


    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null){
            mPresenter.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mPresenter != null){
            mPresenter.onPause();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null){
            mPresenter.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mPresenter != null){
            mPresenter.onBackPressed();
        }
    }
}
